package fr.mehfile.api.update.tasks;


import fr.mehfile.api.database.DataBase;
import fr.mehfile.api.database.DataBaseUpdater;
import fr.mehfile.api.parser.UpdateData;
import fr.mehfile.api.update.Tasks;
import fr.mehfile.api.update.TasksListener;

import android.content.Context;


public class DatabaseUpdateTask extends Tasks
{
	private Context context;
	private UpdateData updateData;
	private DataBase database;
	
	

	public UpdateData getUpdateData()
	{
		return updateData;
	}

	public DatabaseUpdateTask(Context context, TasksListener listener, UpdateData data, DataBase database)
	{
		super(Tasks.UPDATE_DATABASE_TASK_ID, listener);
		this.context = context;
		this.updateData = data;
		this.database = database;
	}

	@Override
	protected Object doInBackground(Object... params)
	{
		Object result=null;
		try {
			database.startTransaction();
			result=DataBaseUpdater.updateDataBase(context, database, updateData);

           
			database.setTransactionSuccessfull();
			
			database.endTransaction();
			
		} catch (Exception e) {
			result=e;
			database.endTransaction();
		}
		return result;
	}
	
	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		
		if(result instanceof Boolean)
		{
			getListener().onComplete(this, result);
			
		}
		if(result instanceof Exception)
		{
			getListener().onError(this,(Exception) result);
			 
		}
	}

}
